import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import NProgress from 'nprogress';

export const routes: Array<RouteRecordRaw> = [
  {
    path: '/AI',
    name: 'AI',
    redirect: '/AI/chat',
    component: () => import('@/layout/AI.vue'),
    children: [
      {
        path: 'chat',
        name: 'chat',
        component: () => import('@/pages/chat/index.vue'),
      },
      {
        path: 'webdisk',
        name: 'webdisk',
        component: () => import('@/pages/disk/web.vue'),
      },
    ],
  },
  {
    path: '/manager/login',
    name: 'mlogin',
    component: () => import('@/pages/auth/MLogin.vue'),
    meta: {
      title: '后台管理登陆',
    },
  },
  {
    path: '/manager/register',
    name: 'mregister',
    component: () => import('@/pages/auth/MRegister.vue'),
    meta: {
      title: '后台管理注册',
    },
  },
  {
    path: '/manager',
    name: 'manager',
    redirect: '/manager/home',
    component: () => import('@/layout/manager/index.vue'),
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import('@/pages/home/home.vue'),
        meta: {
          title: '首页',
        },
      },
      {
        path: 'dashboard',
        name: 'dashboard',
        component: () => import('@/pages/home/dashboard.vue'),
        meta: {
          title: '看板',
        },
      },
      {
        path: 'menu',
        name: 'menu',
        component: () => import('@/pages/home/menu.vue'),
        meta: {
          title: '菜单管理',
        },
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/pages/user/role.vue'),
        meta: {
          title: '角色管理',
        },
      },
    ],
  },
];

const Route = createRouter({
  history: createWebHashHistory(),
  routes,
});

Route.afterEach(to => {
  document.title = (to.meta.title as string) || '默认标题'; // 动态修改title
  NProgress.done(); // 结束进度条
});

Route.beforeEach(() => {
  // 权限检查
  NProgress.start(); // 开启进度条
});

export default Route;
